#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <poll.h>
#include <signal.h>
#include <dirent.h>
#include <pthread.h>
#include <linux/input.h>
#include <linux/fb.h>
#include <json-c/json.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <png.h>
#include <gif_lib.h>
#include "showtime.h"
#include "devinfo.h"

// 全局变量
WeatherData global_weather;
Animation *global_animation = NULL; // 定义 global_animation
Image *global_charging_icon = NULL; // 定义 global_charging_icon
Image *global_battery_icon = NULL;  // 定义 global_battery_icon
Image *global_wsd_icon = NULL;      // 定义 global_wsd_icon
int current_control_state = 1;      // 当前控制状态(1=显示,0=隐藏)
int last_control_state = 1;         // 上次控制状态

// 电池和音量状态变量
int battery_capacity = 50;
int vol_capacity = 50;
char capacity_str[32] = "50";      // 当前控制状态(1=显示,0=隐藏)
char battery_status[32] = "Unknown";         // 上次控制状态
char capacity_str_vol[32] = "音量:50%";         // 上次控制状态
int in_time_mode = 1;  // 时间显示模式状态

// 控制线程函数
static void* control_thread_func(void* arg) {
    while (in_time_mode) {
        last_control_state = current_control_state;
        current_control_state = !should_exit_from_control_file();
        
        // 状态变化时打印日志
        if (current_control_state != last_control_state) {
            printf("控制状态变化: %d -> %d\n", last_control_state, current_control_state);
        }
        
        sleep(1); // 每秒检查一次
    }
    return NULL;
}


int show_time(int argc, char *argv[]) {
    FrameBuffer fb;
    FontRenderer fr;
    Image *charging_icon = NULL;
    Image *battery_icon = NULL;
    
    init_control_file();
    
    if (fb_init(&fb, "/dev/fb0") < 0) return 1;
    if (font_init(&fr, "./wwDigital.ttf") < 0) {
        fb_close(&fb);
        return 1;
    }
    
    
    int in_time_mode = 1;
    int last_state = 1;
    
    while (in_time_mode) {
        // 使用全局变量检查控制状态
        if (current_control_state != last_control_state) {
            if (current_control_state == 0) {
                // 立即清屏
                fb_clear_back_buffer(&fb, 0, 0, 0);
                fb_update_screen(&fb);
            }
            last_control_state = current_control_state;
        }
        
        // 只在显示状态下刷新屏幕
        if (current_control_state) {
            int should_exit = 0;
            show_time_and_battery_with_weather(&fb, &fr, &should_exit, &global_weather);
            if (should_exit) break;
        } else {
            // 非显示状态下降低CPU使用率
            usleep(1000000); // 50ms
        }
        usleep(200000); // 50ms
    }
    
    free_image(charging_icon);
    free_image(battery_icon);
    font_close(&fr);
    fb_close(&fb);
    return 0;
}

static void* show_time_wrapper(void* arg) {
    show_time(0, NULL);
    return NULL;
}

int main_2() {
    static pthread_t time_thread;
    static pthread_t weather_thread;
    static pthread_t control_thread;
    
    weather_init(&global_weather);
    pthread_create(&time_thread, NULL, show_time_wrapper, NULL);
    pthread_create(&weather_thread, NULL, weather_update_thread, &global_weather);
    pthread_create(&control_thread, NULL, control_thread_func, NULL);
    
    pthread_join(time_thread, NULL);
    pthread_join(weather_thread, NULL);
    
    weather_cleanup(&global_weather);
    return 0;
}
